package com.changgou.page.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.entity.Result;
import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.page.service.PageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageServiceImpl implements PageService {

    @Value("${pagepath}")
    private String pagepath;

    @Autowired
    private TemplateEngine templateEngine;

    @Override
    public void generateHtml(String spuId) {
        Map<String, Object> data=this.getData(spuId);
        Context context = new Context();
        context.setVariables(data);

        File file = new File("F:\\html");
        if(!file.exists()){
            file.mkdirs();
        }
        PrintWriter printWriter=null;
        try {
            printWriter = new PrintWriter(new File(file, spuId + ".html"));
            templateEngine.process("item",context,printWriter);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                printWriter.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Autowired
    private CategoryFeign categoryFeign;
    @Autowired
    private SpuFeign spuFeign;
    @Autowired
    private SkuFeign skuFeign;
    public Map<String,Object> getData(String spuId){
        HashMap<String, Object> resultMap = new HashMap<>();

        String s = JSON.toJSONString(spuFeign.findSpuById(spuId).getData());
        Spu spu = JSON.parseObject(s, Spu.class);

        resultMap.put("spu",spu);

        if(spu!=null&& StringUtils.isNotEmpty(spu.getImages())){
            String[] split = spu.getImages().split(",");
            resultMap.put("imageList",split);
        }

        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        resultMap.put("skuList",skuList);

        String s1=JSON.toJSONString(categoryFeign.findById(spu.getCategory1Id()).getData());
        Category category1 = JSON.parseObject(s1, Category.class);
        resultMap.put("category1",category1);

        String s2=JSON.toJSONString(categoryFeign.findById(spu.getCategory2Id()).getData());
        Category category2 = JSON.parseObject(s1, Category.class);
        resultMap.put("category2",category2);

        String s3=JSON.toJSONString(categoryFeign.findById(spu.getCategory3Id()).getData());
        Category category3 = JSON.parseObject(s1, Category.class);
        resultMap.put("category3",category3);
        return resultMap;

    }
}
